//
//  SEPrescriptionOrderListlViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/5/7.
//

import UIKit

class SEPrescriptionOrderListlViewModel: SEBaseViewModel {
    var models: [SEPrescriptionOrderDetailModel] = []

    var cancelHandler: ((SEHttpStatus) -> Void)?

    var prescriptionIds: [String] = []

    func listViewModel(indexPath: IndexPath) -> SEPrescriptionOrderDetailViewModel {
        return SEPrescriptionOrderDetailViewModel(model: models[indexPath.section])
    }

    var selectedViewModel = SEPrescriptionOrderDetailViewModel()

    /// 处方订单列表
    func requestPrescriptionList() {
        SEHttpManager.shared.request(SEProfileAPI.prescriptionOrderList(pageNum: page, pageSize: pageSize, bisIds: prescriptionIds), model: SEPrescriptionOrderListModel.self) { model, status in
            switch status {
            case .success:
                if self.page == 1 {
                    self.models = model?.list ?? []
                } else {
                    self.models.append(contentsOf: model?.list ?? [])
                }
                self.hasMore = self.models.count < model?.total ?? 0
            default:
                self.page == 1 ? (self.page = 1) : (self.page -= 1)
            }
            self.updateData?(status)
        }
    }

    /// 处方订单取消
    func requesetCancelPrescriptionOrder() {
        SEHttpManager.shared.requestForAny(SEProfileAPI.prescriptionOrderCancel(id: selectedViewModel.model.id), withLoading: true) { result, status in
            self.cancelHandler?(status)
        }
    }

}
